
package com.netease.xone.fragment;

import msgCenter.MsgCenter;
import preferMgr.XonePrefHelper;
import protocol.XoneCallBack;
import protocol.XoneService;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.netease.xone.R;

public class FragSetPush extends FragmentBase implements OnCheckedChangeListener {

    private CheckBox mCb1, mCb2, mCb3, mCb4, mCb5, mCb6, mCb7, mCb8;
    private TextView mFrame;
    private long mOriTime;
    private Context mContext;
    private boolean mB1, mB2, mB3, mB4, mB5, mB6, mB7, mB8;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        getActivity().setTitle(R.string.setting_item_push);
        View view = inflater.inflate(R.layout.fragment_push_set, container, false);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        mFrame = (TextView)view.findViewById(R.id.push_set_frame);
        mCb1 = (CheckBox)view.findViewById(R.id.switch_push_check);
        mCb2 = (CheckBox)view.findViewById(R.id.switch_at_check);
        mCb3 = (CheckBox)view.findViewById(R.id.switch_comment_check);
        mCb4 = (CheckBox)view.findViewById(R.id.switch_msg_check);
        mCb5 = (CheckBox)view.findViewById(R.id.switch_fans_check);
        mCb6 = (CheckBox)view.findViewById(R.id.switch_dynamic_check);
        mCb7 = (CheckBox)view.findViewById(R.id.switch_vibrate_check);
        mCb8 = (CheckBox)view.findViewById(R.id.switch_sound_check);

        mCb1.setOnCheckedChangeListener(this);
        mCb2.setOnCheckedChangeListener(this);
        mCb3.setOnCheckedChangeListener(this);
        mCb4.setOnCheckedChangeListener(this);
        mCb5.setOnCheckedChangeListener(this);
        mCb6.setOnCheckedChangeListener(this);
        mCb7.setOnCheckedChangeListener(this);
        mCb8.setOnCheckedChangeListener(this);

         setViewsState();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XoneService.getInstance().addListener(mXoneCallBack);
        mContext = getActivity();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        XoneService.getInstance().removeListener(mXoneCallBack);
    }

    @Override
    public void onPause() {
        super.onPause();
        boolean changed = (mB1 != mCb1.isChecked()) || (mB2 != mCb2.isChecked())
                || (mB3 != mCb3.isChecked()) || (mB4 != mCb4.isChecked())
                || (mB5 != mCb5.isChecked()) || (mB6 != mCb6.isChecked())
                || (mB7 != mCb7.isChecked()) || (mB8 != mCb8.isChecked());
        if (changed) {
            XonePrefHelper.putSettingUpdateTime(mContext, System.currentTimeMillis());
            XoneService.getInstance().doSetAppSetting();
            AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    
                    if (XonePrefHelper.getNeedPush(getActivity().getApplicationContext())) {
                        MsgCenter.getInstance(getActivity().getApplicationContext()).launchService();
                    } else {
                        MsgCenter.getInstance(getActivity().getApplicationContext()).stopService();
                    }
                    return null;
                }
            };
            
            task.execute();
        }
    }

    private void setViewsState() {
        if (mCb8 == null)
            return;
        // mOriTime = XonePrefHelper.getSettingUpdateTime(mContext);
        mB1 = XonePrefHelper.getPushOn(mContext);
        mCb1.setChecked(mB1);
        if (mB1) {
            mFrame.setVisibility(View.GONE);
        } else {
            mFrame.setVisibility(View.VISIBLE);
        }
        mB2 = XonePrefHelper.getAtMeOn(mContext);
        mCb2.setChecked(mB2);
        mB3 = XonePrefHelper.getNewCommentOn(mContext);
        mCb3.setChecked(mB3);
        mB4 = XonePrefHelper.getNewMessageOn(mContext);
        mCb4.setChecked(mB4);
        mB5 = XonePrefHelper.getNewFansOn(mContext);
        mCb5.setChecked(mB5);
        mB6 = XonePrefHelper.getNewFriendDynamicOn(mContext);
        mCb6.setChecked(mB6);
        mB7 = XonePrefHelper.getShockOn(mContext);
        mCb7.setChecked(mB7);
        mB8 = XonePrefHelper.getSoundOn(mContext);
        mCb8.setChecked(mB8);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.switch_push_check:
                if (isChecked) {
                    mFrame.setVisibility(View.GONE);
                } else {
                    mFrame.setVisibility(View.VISIBLE);
                }
                XonePrefHelper.putPushOn(mContext, isChecked);
                break;
            case R.id.switch_at_check:
                XonePrefHelper.putAtMeOn(mContext, isChecked);
                break;
            case R.id.switch_comment_check:
                XonePrefHelper.putNewCommentOn(mContext, isChecked);
                break;
            case R.id.switch_msg_check:
                XonePrefHelper.putNewMessageOn(mContext, isChecked);
                break;
            case R.id.switch_fans_check:
                XonePrefHelper.putNewFansOn(mContext, isChecked);
                break;
            case R.id.switch_dynamic_check:
                XonePrefHelper.putNewFriendDynamicOn(mContext, isChecked);
                break;
            case R.id.switch_vibrate_check:
                XonePrefHelper.putShockOn(mContext, isChecked);
                break;
            case R.id.switch_sound_check:
                XonePrefHelper.putSoundOn(mContext, isChecked);
                break;
        }
        // XonePrefHelper.putSettingUpdateTime(mContext,
        // System.currentTimeMillis());
    }

    private XoneCallBack mXoneCallBack = new XoneCallBack() {

        @Override
        public void onGetAppSetting(int transactionId) {
            setViewsState();
        };
//
//        @Override
//        public void onGetAppSettingError(int transactionId, int errCode, String err) {
//            setViewsState();
//        };

    };
}
